package toman;

/**
 * This interface is implemented by query engines.
 * <p>
 * User: zslajchrt
 * Date: 2.5.11
 * Time: 13:59
 */
public interface QueryEngine {

    /**
     * Determines whether this query engine is capable of finding the raw data for instantiating the given TO class
     * by means of the passed key.
     * @param toman TOMan instance
     * @param toClass the TO class to be instantiated
     * @param toId the key
     * @param <T> the TO type
     * @param <K> the key type
     * @return true/false
     */
    <T, K> boolean canFindInitObject(TOMan toman, Class<T> toClass, K toId);

    /**
     * Finds the raw data for instantiating the given TO class by means of the passed key.
     * @param toman TOMan instance
     * @param toClass the TO class to be instantiated
     * @param toId the key
     * @param <T> the TO type
     * @param <K> the key type
     * @return the raw data for instantiating the TO
     * @throws Exception
     */
    <T, K> Object findInitObject(TOMan toman, Class<T> toClass, K toId) throws Exception;

    /**
     * Determines whether this query engine is capable of creating the query for updating the underlying storage with
     * the passed TO.
     * @param toman TOMan instance
     * @param to the TO
     * @param <T> the TO type
     * @return true/false
     */
    <T> boolean canCreateQueryForUpdate(TOMan toman, T to);

    /**
     * Creates the query for updating the underlying storage with the passed TO.
     * @param toman TOMan instance
     * @param to the TO
     * @param <T> the TO type
     * @return the query for update
     * @throws Exception
     */
    <T> QueryForUpdate createQueryForUpdate(TOMan toman, T to) throws Exception;

    /**
     * Determines whether this query engine is capable of creating the query for the given query definition. The query
     * definition can of a various flavors like an SQL, JPQL or HQL fragment or a a custom object like a complex filter
     * configuration.
     * @param toman TOMan instance
     * @param toClass the TO class to be instantiated
     * @param queryDefinition the query definition
     * @param <T> the TO type
     * @return true/false
     */
    <T> boolean canCreateQuery(TOMan toman, Class<T> toClass, Object queryDefinition);

    /**
     * Creates the query for the given query definition. The query definition can of a various flavors like
     * an SQL, JPQL or HQL fragment or a a custom object like a complex filter configuration.
     * @param toman TOMan instance
     * @param toClass the TO class to be instantiated
     * @param queryDefinition the query definition
     * @param <T> the TO type
     * @return the query
     * @throws Exception
     */
    <T> TOQuery<T> createQuery(TOMan toman, Class<T> toClass, Object queryDefinition) throws Exception;

}
